hbridge18 2.2.1
Loading...
Searching...
No Matches
hbridge18.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2026 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
27
28#ifndef HBRIDGE18_H
29#define HBRIDGE18_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_spi_master.h"
52#include "spi_specifics.h"
53
59
64
69
74#define HBRIDGE18_REG_IC_STAT 0x00
75#define HBRIDGE18_REG_OCP_STAT_1 0x01
76#define HBRIDGE18_REG_OLD_STAT_1 0x04
77#define HBRIDGE18_REG_CONFIG_CTRL 0x07
78#define HBRIDGE18_REG_OP_CTRL_1 0x08
79#define HBRIDGE18_REG_PWM_CTRL_1 0x0B
80#define HBRIDGE18_REG_PWM_CTRL_2 0x0C
81#define HBRIDGE18_REG_FW_CTRL_1 0x0D
82#define HBRIDGE18_REG_PWM_MAP_CTRL_1 0x0F
83#define HBRIDGE18_REG_PWM_MAP_CTRL_2 0x10
84#define HBRIDGE18_REG_PWM_FREQ_CTRL_1 0x13
85#define HBRIDGE18_REG_PWM_DUTY_CTRL_1 0x15
86#define HBRIDGE18_REG_PWM_DUTY_CTRL_2 0x16
87#define HBRIDGE18_REG_PWM_DUTY_CTRL_3 0x17
88#define HBRIDGE18_REG_PWM_DUTY_CTRL_4 0x18
89#define HBRIDGE18_REG_SR_CTRL_1 0x1D
90#define HBRIDGE18_REG_OLD_CTRL_1 0x1F
91#define HBRIDGE18_REG_OLD_CTRL_2 0x20
92#define HBRIDGE18_REG_OLD_CTRL_3 0x21
93#define HBRIDGE18_REG_OLD_CTRL_4 0x22
94#define HBRIDGE18_REG_OLD_CTRL_5 0x23
95#define HBRIDGE18_REG_OLD_CTRL_6 0x24
96 // hbridge18_reg
98
103
108
113#define HBRIDGE18_IC_STAT_OTSD 0x40
114#define HBRIDGE18_IC_STAT_OTW 0x20
115#define HBRIDGE18_IC_STAT_OLD 0x10
116#define HBRIDGE18_IC_STAT_OCP 0x08
117#define HBRIDGE18_IC_STAT_UVLO 0x04
118#define HBRIDGE18_IC_STAT_OVP 0x02
119#define HBRIDGE18_IC_STAT_NPOR 0x01
120
125#define HBRIDGE18_CONFIG_CTRL_POLD_EN 0x80
126#define HBRIDGE18_CONFIG_CTRL_IC_ID_DRV8904 0x40
127#define HBRIDGE18_CONFIG_CTRL_IC_ID_MASK 0x70
128#define HBRIDGE18_CONFIG_CTRL_OCP_REP 0x08
129#define HBRIDGE18_CONFIG_CTRL_OTW_REP 0x04
130#define HBRIDGE18_CONFIG_CTRL_EXT_OVP 0x02
131#define HBRIDGE18_CONFIG_CTRL_CLR_FLT 0x01
132
137#define HBRIDGE18_OP_CTRL_1_HB4_HS_EN 0x80
138#define HBRIDGE18_OP_CTRL_1_HB4_LS_EN 0x40
139#define HBRIDGE18_OP_CTRL_1_HB3_HS_EN 0x20
140#define HBRIDGE18_OP_CTRL_1_HB3_LS_EN 0x10
141#define HBRIDGE18_OP_CTRL_1_HB2_HS_EN 0x08
142#define HBRIDGE18_OP_CTRL_1_HB2_LS_EN 0x04
143#define HBRIDGE18_OP_CTRL_1_HB1_HS_EN 0x02
144#define HBRIDGE18_OP_CTRL_1_HB1_LS_EN 0x01
145#define HBRIDGE18_OP_CTRL_1_HB34_MASK 0xF0
146#define HBRIDGE18_OP_CTRL_1_HB12_MASK 0x0F
147
152#define HBRIDGE18_OLD_CTRL_1_HB4_OLD_DIS 0x08
153#define HBRIDGE18_OLD_CTRL_1_HB3_OLD_DIS 0x04
154#define HBRIDGE18_OLD_CTRL_1_HB2_OLD_DIS 0x02
155#define HBRIDGE18_OLD_CTRL_1_HB1_OLD_DIS 0x01
156
161#define HBRIDGE18_MOTOR_0 0
162#define HBRIDGE18_MOTOR_1 1
163
168#define HBRIDGE18_MOTOR_STATE_COAST 0
169#define HBRIDGE18_MOTOR_STATE_FORWARD 1
170#define HBRIDGE18_MOTOR_STATE_REVERSE 2
171#define HBRIDGE18_MOTOR_STATE_BRAKE_LS 3
172#define HBRIDGE18_MOTOR_STATE_BRAKE_HS 4
173
182#define HBRIDGE18_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
183#define HBRIDGE18_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
184 // hbridge18_set
186
191
196
201#define HBRIDGE18_MAP_MIKROBUS( cfg, mikrobus ) \
202 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
203 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
204 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
205 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
206 cfg.sleep = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
207 cfg.fault = MIKROBUS( mikrobus, MIKROBUS_INT )
208 // hbridge18_map // hbridge18
211
216typedef struct
217{
218 // Output pins
219 digital_out_t sleep;
220
221 // Input pins
222 digital_in_t fault;
223
224 // Modules
225 spi_master_t spi;
226
227 pin_name_t chip_select;
228
229 uint8_t ic_status;
230
232
237typedef struct
238{
239 // Communication gpio pins
240 pin_name_t miso;
241 pin_name_t mosi;
242 pin_name_t sck;
243 pin_name_t cs;
244
245 // Additional gpio pins
246 pin_name_t sleep;
247 pin_name_t fault;
248
249 // static variable
250 uint32_t spi_speed;
251 spi_master_mode_t spi_mode;
252 spi_master_chip_select_polarity_t cs_polarity;
253
255
266
272
283
298
312
324err_t hbridge18_write_reg ( hbridge18_t *ctx, uint8_t reg, uint8_t data_in );
325
337err_t hbridge18_read_reg ( hbridge18_t *ctx, uint8_t reg, uint8_t *data_out );
338
348
358
368
379
398err_t hbridge18_set_motor_state ( hbridge18_t *ctx, uint8_t motor, uint8_t state );
399
400#ifdef __cplusplus
401}
402#endif
403#endif // HBRIDGE18_H
404 // hbridge18
406
407// ------------------------------------------------------------------------ END
err_t hbridge18_check_communication(hbridge18_t *ctx)
H-Bridge 18 check communication function.
void hbridge18_enable_device(hbridge18_t *ctx)
H-Bridge 18 enable device function.
void hbridge18_disable_device(hbridge18_t *ctx)
H-Bridge 18 disable device function.
err_t hbridge18_set_motor_state(hbridge18_t *ctx, uint8_t motor, uint8_t state)
H-Bridge 18 set motor state function.
err_t hbridge18_init(hbridge18_t *ctx, hbridge18_cfg_t *cfg)
H-Bridge 18 initialization function.
err_t hbridge18_write_reg(hbridge18_t *ctx, uint8_t reg, uint8_t data_in)
H-Bridge 18 write register function.
uint8_t hbridge18_get_fault_pin(hbridge18_t *ctx)
H-Bridge 18 get fault pin function.
err_t hbridge18_read_reg(hbridge18_t *ctx, uint8_t reg, uint8_t *data_out)
H-Bridge 18 read register function.
void hbridge18_cfg_setup(hbridge18_cfg_t *cfg)
H-Bridge 18 configuration object setup function.
err_t hbridge18_default_cfg(hbridge18_t *ctx)
H-Bridge 18 default configuration function.
hbridge18_return_value_t
H-Bridge 18 Click return value data.
Definition hbridge18.h:261
@ HBRIDGE18_OK
Definition hbridge18.h:262
@ HBRIDGE18_ERROR
Definition hbridge18.h:263
This file contains SPI specific macros, functions, etc.
H-Bridge 18 Click configuration object.
Definition hbridge18.h:238
pin_name_t sleep
Definition hbridge18.h:246
spi_master_chip_select_polarity_t cs_polarity
Definition hbridge18.h:252
pin_name_t sck
Definition hbridge18.h:242
spi_master_mode_t spi_mode
Definition hbridge18.h:251
pin_name_t mosi
Definition hbridge18.h:241
uint32_t spi_speed
Definition hbridge18.h:250
pin_name_t miso
Definition hbridge18.h:240
pin_name_t cs
Definition hbridge18.h:243
pin_name_t fault
Definition hbridge18.h:247
H-Bridge 18 Click context object.
Definition hbridge18.h:217
digital_out_t sleep
Definition hbridge18.h:219
uint8_t ic_status
Definition hbridge18.h:229
spi_master_t spi
Definition hbridge18.h:225
digital_in_t fault
Definition hbridge18.h:222
pin_name_t chip_select
Definition hbridge18.h:227